Skip to content

feat(metrics): implement metric reader metrics#4970

Open
anuraaga wants to merge 10 commits intoopen-telemetry:mainfrom
anuraaga:metrics-reader-metrics
Open

feat(metrics): implement metric reader metrics#4970
anuraaga wants to merge 10 commits intoopen-telemetry:mainfrom
anuraaga:metrics-reader-metrics

Conversation

@anuraaga
Copy link
Contributor

@anuraaga anuraaga commented Mar 12, 2026

Description

I am helping to implement SDK internal metrics

https://opentelemetry.io/docs/specs/semconv/otel/sdk-metrics/

This implements the metric for metric reader collections.

Similar PR in Java: open-telemetry/opentelemetry-java#8038
Similar PR in JS: open-telemetry/opentelemetry-js#6449

/cc @xrmx to help with review

Type of change

Please delete options that are not relevant.

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

How Has This Been Tested?

Please describe the tests that you ran to verify your changes. Provide instructions so we can reproduce. Please also list any relevant details for your test configuration

  • Unit tests

Does This PR Require a Contrib Repo Change?

  • Yes. - Link to PR:
  • No.

Checklist:

  • Followed the style guidelines of this project
  • Changelogs have been updated
  • Unit tests have been added
  • Documentation has been updated

@anuraaga anuraaga requested a review from a team as a code owner March 12, 2026 05:06
@aabmass aabmass moved this to Easy to review / merge / close in Python PR digest Mar 12, 2026
@anuraaga
Copy link
Contributor Author

Sent open-telemetry/opentelemetry-python-contrib#4332 to make genai test only read its scope

@anuraaga anuraaga force-pushed the metrics-reader-metrics branch from 120c3db to 3c70e2a Compare March 13, 2026 05:27
raise Exception(f"Invalid instrument class found {typ}")

self._otel_component_type = (
otel_component_type or type(self).__qualname__
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe there are known values for otel.component.type that must be used if applied.

e.g. periodic_metric_reader

In addition, perhaps we can also be consistent and apply underscore case for other defaults like memory_metric_reader.

) -> None:
meter = meter_provider.get_meter("opentelemetry-sdk")

count = _component_counter[component_type]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need a lock here or would that be overkill since in practice MetricReaders are usually instantiated on application start?

]
| None = None,
*,
otel_component_type: str | None = None,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
otel_component_type: str | None = None,
otel_component_type: OtelComponentTypeValues | None = None,

metric_reader._set_collect_callback(
self._measurement_consumer.collect
)
metric_reader._set_meter_provider(self)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems to be introducing a new pattern of setting the meter provider instead of explicitly passing in one and then defaulting to the global. See tracer metrics and logger metrics. Should we do something similar to be consistent?

metrics = self._collect(self, timeout_millis=timeout_millis)
start_time = perf_counter()
try:
metrics = self._collect(self, timeout_millis=timeout_millis)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like the spec indicates that we should capture error.type as well on a failed collection.

@lzchen
Copy link
Contributor

lzchen commented Mar 16, 2026

@anuraaga

Thanks for your contribution! Could you update the title + description to include that this PR covers specifically implementing the otel.sdk.exporter.operation.duration metric for the metric reader.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Easy to review / merge / close

Development

Successfully merging this pull request may close these issues.

3 participants